// pages/mine/myinfo/index.js
const app = getApp().globalData
import {
  setProfile,
  getUserInfo,
  getMobile
} from '../../../api/index'
import {
  upUser,
  getUser
} from '../../../api/user'
Page({
  data: {
    userInfo: {},
    formMark: true,

    canuse: false,
    // unrender
    oss: '',
    code: '', // wx.login.code
    intnum: '', // 定时获取code

    oid: '', // 订单id，同时修改订单信息
  },
  onLoad: function (e) {
    // this.data.oid = e.oid
    if (wx.getUserProfile) {
      this.setData({
        canuse: true
      })
    }
    // this.getOssParams('avatar')
    // getUserInfo({}).then((res) => {
    //   this.setData({ userInfo: res.datas, formMark: true })
    // })
    // this.getCode()
    // this.data.intnum = setInterval(() => {
    //   this.getCode()
    // }, 5 * 60 * 1000);
    getUser({}).then((res) => {
      this.setData({
        userInfo: res.datas,
        formMark: true
      })
    })
  },
  bindgetuserinfo(e) {
    if (e.detail.errMsg.includes('ok')) {
      const p = {
        code: this.data.code,
        iv: e.detail.iv,
        encryptedData: e.detail.encryptedData
      }
      getMobile(p).then((res2) => {
        let userInfo = this.data.userInfo
        userInfo.nickname = res2.datas.nickName
        userInfo.avatar = res2.datas.avatarUrl
        this.setData({
          userInfo
        })
        this.getCode()
        let p2 = {
          avatar: this.data.userInfo.avatar,
          name: this.data.userInfo.name,
          nickname: this.data.userInfo.nickname,
          phone: this.data.userInfo.phone,
          address: this.data.userInfo.address
        }
        setProfile(p2)
      })
    }
  },
  getCode() {
    wx.login({
      success: (res) => {
        if (res.code) {
          this.data.code = res.code
        }
      }
    })
  },
  getWxUserInfo() {
    if (!this.data.canuse) {
      return
    }
    wx.getUserProfile({
      desc: "获取你的昵称、头像789",
      success: res => {
        console.log(res);
        let userInfo = this.data.userInfo
        userInfo.nickname = res.userInfo.nickName
        userInfo.avatar = res.userInfo.avatarUrl
        this.setData({
          userInfo
        })
        let p2 = {
          avatar: this.data.userInfo.avatar,
          name: this.data.userInfo.nickname,
          nickname: this.data.userInfo.nickname,
        }
        setProfile(p2)
      },
    })
  },
  saveInfo() {
    if (this.data.bChild) {
      this.saveChild()
      return
    }
    let p = {
      avatar: this.data.userInfo.avatar,
      name: this.data.userInfo.name,
      nickname: this.data.userInfo.nickname,
      phone: this.data.userInfo.phone,
      address: this.data.userInfo.address
    }
    let str = ''
    if (!p.phone) {
      str = '请输入手机号'
    }
    if (str) {
      wx.showToast({
        icon: 'none',
        title: str
      })
      return
    }
    upUser(p).then((res) => {
      app.isReload = true
      wx.showToast({
        icon: 'none',
        title: res.message,
      })
      setTimeout(() => {
        wx.navigateBack()
      }, 1000);
    })
    if (this.data.oid) {
      editOrder({
        order_id: this.data.oid,
        ...p
      })
    }
  },
  uploadImg(e) {
    var _this = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        var tempFilePaths = res.tempFilePaths
        _this.uploadFile(tempFilePaths[0], e.currentTarget.dataset.t)
      }
    })
  },
  uploadFile(it, key) {
    wx.showLoading({
      title: '上传中...'
    })
    wx.uploadFile({
      url: app.domain + 'upload',
      filePath: it,
      name: "file",
      header: {
        "Content-Type": "multipart/form-data"
      },
      formData: {
        name: it,
      },
      success: (res) => {
        wx.hideLoading()
        const t = JSON.parse(res.data)
        this.setData({
          ['userInfo.avatar']: t.datas.url
        })
      }
    })
  },
  conInput(e) {
    const {
      k
    } = e.currentTarget.dataset
    this.setData({
      ['userInfo.' + k]: e.detail.value
    })
  },
  bindgetphonenumber(e) {
    console.log(e);
    if (e.detail.errMsg.includes('ok')) {
      const p = {
        code: this.data.code,
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv
      }
      getMobile(p).then((res2) => {
        this.setData({
          ['userInfo.mobile']: res2.datas.phoneNumber
        })
        const p = {
          mobile: res2.datas.phoneNumber
        }
        this.getCode()
        setProfile(p)
      })
    }
  },
  onUnload() {
    this.data.intnum && clearInterval(this.data.intnum)
  },
  onShow: function () {

  },
})